<?php
class Psr4AutoLoad
{
	protected $namespaces = array();

	public function register()
	{
		spl_autoload_register(array($this , 'loadClass'));
	}

	public function loadClass($className)
	{
		//echo $className;die;
		$pos = strrpos($className, '\\');

		$namespace = substr($className , 0 , $pos+1);

		$realClassName = substr($className , $pos+1);

		$this->mapLoad($namespace , $realClassName);
	}

	public function mapLoad($namespace , $realClassName)
	{
			//两种情况 一种 
		if (isset($this->namespaces[$namespace]) == false) {
			$fileName = str_replace('\\' , '/' , strtolower($namespace . $realClassName . '.php'));
		} else {
			foreach ($this->namespaces[$namespace] as $path) {
				

				$fileName = strtolower($path . '/' . $realClassName . '.php');

			}
		}

		$this->requireFile($fileName);
	}

	//添加命名空间
	public function addNamespace($namespace , $path)
	{
		$this->namespaces[$namespace][] = $path;
	}

	public function requireFile($fileName)
	{
		if (file_exists($fileName)) {
			include $fileName;
			return true;
		}

		return false;
	}
}